package com.jwdf.uda.ui.fragment.user.personal;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentIndividualInfoBinding;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UserInfoAction;
import com.jwdf.uda.http.action.UserUpdateInfoAction;
import com.jwdf.uda.ui.activity.AccountSecurityActivity;
import com.jwdf.uda.ui.activity.ChangeAvatarActivity;
import com.jwdf.uda.ui.activity.ChangeNickNameActivity;
import com.jwdf.uda.ui.activity.ChangeUserNameActivity;
import com.jwdf.uda.ui.activity.IndividualitySignatureActivity;
import com.jwdf.uda.ui.activity.SetCoverActivity;
import com.jwdf.uda.ui.activity.UserSexActivity;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.DateUtil;
import com.jwdf.uda.utils.DateUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.view.dialog.ChoiceDialog;
import com.jwdf.uda.view.dialog.ConfirmDialog;
import com.jwdf.uda.view.picker.TimeSelector;
import com.jwdf.uda.view.timePicket.TimePickerShow;
import com.nostra13.universalimageloader.core.ImageLoader;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/6/21.
 */
public class IndividualInfoFragment extends BaseFragment {
    private final static int CODE_OLD_PASS = 1;
    private final static int CODE_MOBILE = 2;
    private final static int CODE_CANCEL = 3;
    private static final int MSG_AUTH_COMPLETE = 1;
    private static final int MSG_AUTH_CANCEL = 2;
    private static final int MSG_AUTH_ERROR = 3;
    private FragmentIndividualInfoBinding mDataBinding;
    private ChoiceDialog mChoiceDialog;
    private ConfirmDialog mWechatDialog;
    private ConfirmDialog mWeiboDialog;
    private PbBean.PbUserInfo userInfo;
    private TimeSelector timeSelector;
    private TimePickerShow timePickerShow;
    private final String FORMAT_STR = "yyyy年MM月dd日";
    private String mDataTime;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding = FragmentIndividualInfoBinding.inflate(inflater, container, false);
        initViews();
        return mDataBinding.getRoot();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mDataBinding.setIndividualInfoFragment(this);
    }

    //    @Override
    //    public void onResume() {
    //        super.onResume();
    //        closeProgressDialog();
    //    }

    private void initViews() {
        userInfo = (PbBean.PbUserInfo) getArguments().getSerializable("user_info");
        loadUserInfo(userInfo);
    }

    private void loadUserInfo(PbBean.PbUserInfo userInfo) {
        timePickerShow = new TimePickerShow(getContext());
        mDataTime=userInfo.getConstellation().getNumber() != 0 ? userInfo.getBirthday(): DateUtils.getCurrentTime();
        mDataBinding.tvDate.setText(userInfo.getConstellation().getNumber() != 0 ? userInfo.getBirthday() +
                "  " + DateUtil.getConstellations(userInfo.getConstellation().getNumber()) : getString(R.string.account_none_name));
//        timeSelector = new TimeSelector(getContext(), new TimeSelector.ResultHandler() {
//            @Override
//            public void handle(String times, String time, int month, int day) {
//                mDataBinding.tvDate.setText(times + "  " + DateUtil.getConstellation(month, day));
//                updateBirthday(times, PbBean.PbSignUserConstellation.USER_CONSTELLATION_NONE);
//            }
//        }, "1985-01-01 00:00", DateUtils.getCurrentTime());

        timePickerShow.setOnTimePickerListener(new TimePickerShow.OnTimePickerListener() {
            @Override
            public void onClicklistener(String dataTime) {
                updateBirthday(dataTime, PbBean.PbSignUserConstellation.USER_CONSTELLATION_NONE);
            }

        });

        mDataBinding.tvUserName.setText(userInfo.getIsVisitor() ? getString(R.string.account_none_name) : userInfo.getUsername());
        mDataBinding.ivArrowUsr.setVisibility(userInfo.getIsVisitor() ? View.VISIBLE : View.GONE);
        ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), mDataBinding.ivAvatar);
        mDataBinding.rlUserAvatar.setTag(R.id.tag_url, userInfo.getAvatar().getUrl());
        mDataBinding.rlCover.setTag(R.id.tag_url, userInfo.getCover().getUrl());
        mDataBinding.tvNickName.setText(userInfo.getNickname());
        if (userInfo.getSex().ordinal() == PbBean.PbSignUserSex.USER_SEX_MAN.ordinal()) {
            mDataBinding.tvSex.setText(getString(R.string.male));
        } else if (userInfo.getSex().ordinal() == PbBean.PbSignUserSex.USER_SEX_WOMAN.ordinal()) {
            mDataBinding.tvSex.setText(getString(R.string.female));
        } else {
            mDataBinding.tvSex.setText(getString(R.string.account_none_name));
        }
        this.userInfo = userInfo;
    }

    public void onUserNameClick(View view) {
        String usr = mDataBinding.tvUserName.getText().toString();
        if (TextUtils.equals(usr, getString(R.string.account_none_name))) {
            ChangeUserNameActivity.start(view.getContext());
        }
    }

    public void onUserAvatarClick(View view) {
        RelativeLayout avatar = (RelativeLayout) view;
        ChangeAvatarActivity.start(view.getContext(), (String) avatar.getTag(R.id.tag_url));
    }

    public void onUserNickNameClick(View view) {
        ChangeNickNameActivity.start(view.getContext(), mDataBinding.tvNickName.getText().toString());
    }

    public void onUserSexClick(View view) {
        String sex = mDataBinding.tvSex.getText().toString();
        UserSexActivity.start(view.getContext(), sex.substring(2, sex.length() - 1));
    }

    public void onCoverClick(View view) {
        RelativeLayout avatar = (RelativeLayout) view;
        SetCoverActivity.start(view.getContext(), (String) avatar.getTag(R.id.tag_url));
    }


    public void onAccountSecurityClick(View view) {
        AccountSecurityActivity.start(view.getContext(), userInfo);
    }

    public void onIndividualitySignatureClick(View view) {
        IndividualitySignatureActivity.start(getContext(), userInfo.getIntroduction());
    }

    public void onUserDateClick(View view) {
//        timeSelector.setIsLoop(true);
//        timeSelector.show();
        timePickerShow.timePickerAlertDialog(mDataTime);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
    }

    @Override
    public void onDestroy() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroy();
    }

    public void onEvent(IndividualEvent event) {
        if (event.userInfo != null) {
            loadUserInfo(event.userInfo);
        } else {
            requestUserInfo();
        }
    }

    private void requestUserInfo() {
        onNewRequestCall(UserInfoAction.newInstance(getContext()).request(new AHttpService.IResCallback<PbBean.PbResUserGetInfo>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserGetInfo> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_operation))) {
                        return;
                    }
                    PbBean.PbResUserGetInfo body = response.body();
                    if (body != null) {
                        PbBean.PbUserInfo userInfo = body.getUserInfo();
                        loadUserInfo(userInfo);
                    }
                }
            }
        }));
    }

    private void updateBirthday(String birthday, PbBean.PbSignUserConstellation userConstellation) {
        onNewRequestCall(UserUpdateInfoAction.newInstance(getContext(), birthday, userConstellation).request(new AHttpService.IResCallback<PbBean.PbResUserUpdateInfo>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserUpdateInfo> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        return;
                    }
                    PbBean.PbResUserUpdateInfo info = response.body();
                    loadUserInfo(info.getUserInfo());
                    IndividualEvent event = new IndividualEvent(IndividualEvent.USER_AVATAR_EVENT);
                    event.userInfo = info.getUserInfo();
                    EventBusUtil.post(event);
                }
            }
        }));
    }


}
